<?php
class RangosController extends AppController{
	var $name = 'Rangos';
	var $components = array('Paginator');
	var $helpers = array('Html', 'Form');
	
	function index(){
		$this->paginate = array(
				'order' => 'Rango.id',
				'conditions' => array('Rango.Deleted' => 0),
				'prefix' => 'Rango',
				'limit' => 10
		);
		
		$this->set('rangos', $this->Paginator->paginate($this->Rango));
	}
	
	function add(){
		$niveles = $this->Rango->Niveles->find('list', array('fields' => 'Niveles.nombre', 'order' => 'Niveles.id'));
		$this->set('niveles', $niveles);
		
		if(!empty($this->data)){
			
			$rango = array();
			$this->Rango->create();
			$this->Rango->id = null;
			$rango['Rango']['nombre'] = $this->data['Rango']['nombre'];
			$rango['Rango']['niveles_id'] = $this->data['Rango']['niveles_id'];
			$rango['Rango']['combates'] = $this->data['Rango']['combates'];
			$rango['Rango']['logo'] = $this->data['Rango']['logo']['name'];
			
			if($this->Rango->save($rango)){
				$this->Session->setFlash('El rango ha sido ingresado correctamemte.');
				$this->redirect(array('action' => 'index'), null, true);			
			} 
			else{
				$this->Session->setFlash('Rango no ingresado. Pruebe de nuevo.');
			}
		}
	}

	function edit($id = null){
		$niveles = $this->Rango->Niveles->find('list', array('fields' => 'Niveles.nombre', 'order' => 'Niveles.id'));
		$this->set('niveles', $niveles);
		$this->set('id', $id);
		
		if(!$id){
			$this->Session->setFlas('Rango Invalido');
			$this->redirect(array('action'=>'index'), null, true);
		}
		
		if(empty($this->data)){
			$this->data = $this->Rango->find('all', array('recursive' => 1, 'conditions' => array('Rango.id' => $id)));	
			$this->data = $this->data['0'];
		}
		else{
			$rango = array();
			$this->Rango->id = null;
			$rango['Rango']['id'] = $this->data['Rango']['id'];
			$rango['Rango']['nombre'] = $this->data['Rango']['nombre'];
			$rango['Rango']['niveles_id'] = $this->data['Rango']['niveles_id'];
			$rango['Rango']['combates'] = $this->data['Rango']['combates'];
			$rango['Rango']['logo'] = $this->data['Rango']['logo']['name'];
			
			if($this->Rango->save($rango)){
				$this->Session->setFlash('El rango ha sido editado');
				$this->redirect(array('action' => 'index'), null, true);
			}
			else{
				$this->Session->setFlash('El rango no ha podido ser editado, intentelo de nuevo');
			}
		}
	}

	function delete($id = null){
		if(!$id){
			$this->Session->setFlash('id invalido para el Rango');
			$this->redirect(array('action' => 'index'), null, true);
		}
		else{
			$rango = array();
			$this->Rango->id = null;
			$rango['Rango']['id'] = $id;
			$rango['Rango']['deleted'] = 1;
			if($this->Rango->save($rango)){
				$this->Session->setFlash('Rango #' . $id . ' borrado');	
				$this->redirect(array('action' => 'index'), null, true);
			}
		}
	}
	
	function reporte(){
		$rangos = $this->Rango->find('all');	
		
		header('Content-type: application/vnd.ms-excel');
			header("Content-Disposition: attachment; filename=\"Reporte_Rangos.xls\"");
			header("Pragma: no-cache");
			header("Expires: 0");
echo <<<html
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html>
<head>
  <title>eClass</title>
  <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
</head>
<body>
html;

			echo '<table border="1">'
			. '<tr>'
			. '<th>Id</th>'
			. '<th>Nombre</th>'
			. '<th>Nivel</th>'
			. '<th>Numero de Combates</th>'
			. '</tr>'
			;

			if (empty($rangos)) {
			echo '
			<tr>
				<td colspan="5" class="center">' . __('No se encontraron registros.', true) . '</td>
			</tr>';
			}
			else {
				foreach ($rangos as $rango) {
					echo ' 
						<tr>
							<td class="center">' . $rango['Rango']['id'] . '</td>
							<td class="center">' . $rango['Rango']['nombre'] . '</td>
							<td class="center">' . $rango['Niveles']['nombre'] . '</td>
							<td class="center">' . $rango['Rango']['combates'] . '</td>
					</tr>';
				}
			}
			echo '</table>';
echo <<<html
</body>
</html>
html;

	}
}
?>